package com.zyp.shop.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

/**
 * Created by Intellij IDEA<br>
 * 用于获取req与session的工具类。
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/10/10 14:41
 */
public class WebUtil {
    /**
     * 获取Session工具方法
     * @return 返回Session
     */
    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    /**
     * 获取Request
     * @return 返回Session
     */
    public static HttpServletRequest getRequest(){
        return ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
    }
}
